package com.ndhutu.ee.util.netease.call;

import com.alibaba.fastjson.JSONObject;
import com.ndhutu.ee.util.netease.App;
import com.ndhutu.ee.util.netease.NeteaseUtil;
import okhttp3.Response;

import java.util.Map;

/**
 * @author shane
 * @version 1.0.0
 * @Date 2021/1/15 15:00
 */
public class CallUtil {
    /**
     * 查询房间状态信息
     * @Author Shane
     * @Date 2020/3/4 23:57
     * @param cid   房间id
     * @return
     */
    public static Map<String, Object> rooms(String cid, App app) throws Exception {
        String url = "https://roomserver-dev.netease.im/v1/api/rooms/"+cid;
        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Response response = NeteaseUtil.toResponse(url, null, headers, NeteaseUtil.contentType_JSON, "GET");
        if(response.code() != 200){
            throw new Exception("查询房间"+cid+"状态失败");
        }
        return JSONObject.parseObject(response.body().string(), Map.class);
    }

    /**
     * 查询某个房间成员信息
     * @Author Shane
     * @Date 2020/3/4 23:57
     * @param cid   房间id
     * @return
     */
    public static String members(String cid, App app) throws Exception {
        String url = "https://roomserver-dev.netease.im/v1/api/rooms/"+cid+"/members";
        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Response response = NeteaseUtil.toResponse(url, null, headers, NeteaseUtil.contentType_JSON, "GET");
        if(response.code() != 200){
            throw new Exception("查询房间"+cid+"成员信息失败");
        }
        return response.body().string();
    }


    /**
     * 踢出房间内的指定用户
     * @Author Shane
     * @Date 2019/11/2 16:13
     * @param cid   房间id
     * @param accid    用户标识
     * @return
     */
    public static void kickMembers(String cid, String accid, App app) throws Exception {
        String url = "https://roomserver-dev.netease.im/v1/api/kicklist/"+cid+"/members/"+accid;
        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Response response = NeteaseUtil.toResponse(url, null, headers, NeteaseUtil.contentType_JSON, "POST");
        if(response.code() != 200){
            throw new Exception("将用户踢出房间失败");
        }
    }

    /**
     * 删除房间
     * @Author Shane
     * @Date 2020/4/8 13:01
     * @param
     * @return
     */
    public static void deleteRoom(String cid, App app) throws Exception{
        String url = "https://roomserver-dev.netease.im/v1/api/rooms/"+cid;
        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Response response = NeteaseUtil.toResponse(url, null, headers, NeteaseUtil.contentType_JSON, "DELETE");
        if(response.code() != 200){
            throw new Exception("删除房间失败");
        }
    }

    public static void main(String[] args) throws Exception {
        //prod
        App prodApp = new App() {
            @Override
            public String getIdPrefix() {
                return "";
            }

            @Override
            public String getAppKey() {
                return "f1b8f63d111c4c45cf417c9ff6445eaf";
            }

            @Override
            public String getAppSecret() {
                return "1c8f32682b1f";
            }
        };

        //dev
        App devApp = new App() {
            @Override
            public String getIdPrefix() {
                return "";
            }

            @Override
            public String getAppKey() {
                return "f31308a092ca2a9afaa474af066c1935";
            }

            @Override
            public String getAppSecret() {
                return "d0c8bd355963";
            }
        };

        Map<String, Object> map = rooms("52787911854033", devApp);
        System.out.println(JSONObject.toJSONString(map));
    }
}
